package org.example.config;

import org.example.filter.CORSFilter;
import org.example.interceptors.FmFrontedJwtTokenInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

@Configuration
public class FmFrontedWebConfig implements WebMvcConfigurer {
    private FmFrontedJwtTokenInterceptor fmFrontedJwtTokenInterceptor;

    public FmFrontedWebConfig(FmFrontedJwtTokenInterceptor fmFrontedJwtTokenInterceptor) {
        this.fmFrontedJwtTokenInterceptor = fmFrontedJwtTokenInterceptor;
    }

    @Bean
    public FilterRegistrationBean<CORSFilter> corsFilterFilterRegistrationBean(){
        FilterRegistrationBean<CORSFilter> bean=new FilterRegistrationBean<>();
        bean.setFilter(new CORSFilter());
        bean.setUrlPatterns(Arrays.asList("/*"));
        bean.setOrder(1);
        return bean;
    }
    //添加拦截器

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(fmFrontedJwtTokenInterceptor)
                .addPathPatterns("/shoppingCart/**","/order/**","/pay","/wxPay")//拦截什么
                .excludePathPatterns("/shoppingCart/count","/orderNum/**");//不拦什么
    }
}
